// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 3 - Number of state when talked to. Plays default text if 0.

begincreaturescript;

variables;

short i,target;
short adj_res = 0;
short which_ghost;

body;

beginstate INIT_STATE;
	set_name(ME,"Essence Shade");
	set_level(ME,16);
	change_max_health(ME,50);
	set_boss_level(ME,1);
	
	set_resistance(ME,0,100);
	set_resistance(ME,1,100);
	set_resistance(ME,2,100);
	set_resistance(ME,3,100);
	set_resistance(ME,4,100);
	set_resistance(ME,5,100);
	set_resistance(ME,6,100);
	set_resistance(ME,7,100);
	set_char_unkillable(ME,1);
	
	which_ghost = my_number() - 8;
	break;

beginstate DEAD_STATE;
	inc_flag(32,9,1);
	if (gf(32,9) > 2) {
		sf(32,3,3);
		sf(32,4,1);
		award_party_xp(100,20);
		begin_talk_mode(60);
		}
break;

beginstate START_STATE; 
	if ((gf(32,3) > 1) && (adj_res == 0)) {
		adj_res = 1;
		set_resistance(ME,0,50);
		set_resistance(ME,1,0);
		set_resistance(ME,2,0);
		set_resistance(ME,3,0);
		set_resistance(ME,6,50);
		}
	
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (gf(32,3) == 1) {
		if (gf(32,6) == 0) {
			if (which_ghost == 0) {
				if (dist_to_zone_loc(ME,22,10) > 1)
					approach_zone_loc(ME,22,10,1);
				}
			if (which_ghost == 1) {
				if (dist_to_zone_loc(ME,24,8) > 1)
					approach_zone_loc(ME,24,8,1);
				}
			if (which_ghost == 2) {
				if (dist_to_zone_loc(ME,26,10) > 1)
					approach_zone_loc(ME,26,10,1);
				}
			}
		if ((gf(32,6) > 0) && (gf(32,7) == 0)) {
			if (which_ghost == 0) {
				if (dist_to_zone_loc(ME,47,53) > 1)
					approach_zone_loc(ME,47,53,1);
				}
			if (which_ghost == 1) {
				if (dist_to_zone_loc(ME,49,55) > 1)
					approach_zone_loc(ME,49,55,1);
				}
			if (which_ghost == 2) {
				if (dist_to_zone_loc(ME,51,53) > 1)
					approach_zone_loc(ME,51,53,1);
				}
			}
		if (gf(32,7) > 0) {
			if (which_ghost == 0) {
				if (dist_to_zone_loc(ME,20,48) > 1)
					approach_zone_loc(ME,20,48,1);
				}
			if (which_ghost == 1) {
				if (dist_to_zone_loc(ME,22,46) > 1)
					approach_zone_loc(ME,22,46,1);
				}
			if (which_ghost == 2) {
				if (dist_to_zone_loc(ME,24,48) > 1)
					approach_zone_loc(ME,24,48,1);
				}
			}
		}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((gf(32,3) > 1) && (adj_res == 0)) {
		adj_res = 1;
		set_resistance(ME,0,50);
		set_resistance(ME,1,0);
		set_resistance(ME,2,0);
		set_resistance(ME,3,0);
		set_resistance(ME,6,50);
		}
		
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((gf(32,3) <= 1) && (get_attitude(ME) >= 10) && (get_ran(1,0,100) < 50)) {
		print_str_color("The shade decides to stop attacking you. Perhaps it forgot that it",2);
		print_str_color("  was fighting you.",2);
		set_foe_target(ME,-1);
		set_attitude(ME,4);
		set_state(START_STATE);
		
		}

	do_attack();
break;

beginstate TALKING_STATE;
	begin_talk_mode(11);
break;